﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    public class PMFDataControl : UserControl
    {
        #region 显示的内容
        public FieldInstance DataSource
        {
            get { return (FieldInstance)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSourceProperty", typeof(FieldInstance), typeof(PMFDataControl), new PropertyMetadata(default(FieldInstance), OnSelectedDictionaryDataPropertyChangedCallback));

        static void OnSelectedDictionaryDataPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PMFDataControl control = d as PMFDataControl;
            control.OnDataSourceChanged(control.DataSource);
        }
        #endregion

        #region 是否只读
        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(PMFDataControl), new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PMFDataControl ctrl = d as PMFDataControl;
            ctrl.OnIsReadOnlyChanged((bool)e.NewValue);
        }
        #endregion

        public bool IsSaveNullable { get; set; }
        public bool IsSubmitNullable { get; set; }
        public virtual void UpdateSource()
        {
        }

        public void ValidateFailure()
        {
            this.Background = new SolidColorBrush(Colors.Yellow);
        }

        public void ValidateSuccess()
        {
            this.Background = new SolidColorBrush(Colors.White);
        }

        protected virtual void OnIsReadOnlyChanged(bool isReadOnly)
        {
            this.IsEnabled = !isReadOnly;
        }

        protected virtual void OnDataSourceChanged(FieldInstance dataSource)
        {
        }
    }
}
